package App.Model.Tool;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;

//适用于控制台
public class DosTool {
    //读取字符串"num"个数字
    public static String readString(int num) {
        Scanner scanner = new Scanner(System.in);
        String data = scanner.nextLine();
        if (data.length() > num) {
            data = data.substring(0, num);
        }
        return data;
    }

    public static String swapFileLength(long fileLength){
        String[] datatype = {"byte","KB","MB","GB","TB"};
        int nP = 0;
        while (fileLength > 1024){
            nP++;
            fileLength = fileLength/1024;
        }
        if(fileLength == 0)
            fileLength = 1;
        return "" + fileLength + datatype[nP];
    }

    public static String SplitFileName(String filePath){
        String[] sArr = filePath.split("\\\\");
        return sArr[sArr.length - 1];
    }

    public static String GetFileType(String filePath){
        String[] sArr = filePath.split("\\.");
        return sArr[sArr.length - 1];
    }

    public static File GetCorrectFilename(String path, String fileName){
        Path directory = Paths.get(path);
        File file = new File(path);
        //用数组把文件夹下的文件存起来
        File[] files = file.listFiles();
        if(files == null)
            return null;
        for (File eF : files) {
            String[] sArr = eF.getName().split("\\.");
            if(fileName.equals(sArr[0]))
                return eF;
        }
        return null;
    }
}
